package com.zhihuishu.micro.course.service.impl;

import com.zhihuishu.micro.course.dto.LessonDto;
import com.zhihuishu.micro.course.helper.LogModule;
import com.zhihuishu.micro.course.helper.RemoteLogHelper;
import com.zhihuishu.micro.course.service.*;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

@Service("lessonAllService")
public class LessonAllServiceImpl extends BaseTreenityBatchServiceImpl<LessonDto> implements LessonTreenityService {
    private final static String moduleCN = "节";
    @Resource
    private LessonTreenityDBService lessonAllServiceDB;

    @Resource
    private LessonTreenityRedisService lessonAllServiceRedis;

    @Override
    protected TreenityBatchDBService<LessonDto, ?> getDBService() {
        return lessonAllServiceDB;
    }

    @Override
    protected Class<LessonDto> getDTOClass() {
        return LessonDto.class;
    }

    @Override
    protected TreenityBatchRedisService<LessonDto> getRedisService() {
        return lessonAllServiceRedis;
    }

    @Override
    public void clearVideoInfo(LessonDto dto, long updateUserId) {
        Assert.notNull(dto);
        Integer dtoId = dto.getId();
        Assert.notNull(dtoId);

        // 更新数据库中的章
        lessonAllServiceDB.overwriteVideoInfo(dto,updateUserId);

        //直接查库，然后更新缓存
        LessonDto detailInDB = getDBService().detail(dtoId);
        log.debug("update {} from params:[{}]!" ,getDTOName(),String.valueOf(dto)) ;
        log.debug("update {} from db:[{}]!" ,getDTOName(),String.valueOf(detailInDB) ) ;

        getRedisService().update(detailInDB,updateUserId);
        log.warn("执行更新dto:[{}]，更新人[{}]!" , dtoId,updateUserId) ;
    }

    @Override
    public Integer create(LessonDto lesson, long creator) {
        Assert.notNull(lesson, moduleCN + "对象为空!");
        Assert.notNull(lesson.getCourseId(), moduleCN + "所属课程ID为空!");
        Assert.notNull(lesson.getChapterId(), moduleCN + "所属章ID为空!");
        Assert.notNull(lesson.getRank(), moduleCN + "序号为空!");
        RemoteLogHelper.log(LogModule.CHAPTER_LESSON.getCode(), creator, "[开始]创建" + moduleCN + "[节ID：{}/序号：{}]", lesson.getChapterId(), lesson.getRank());

        Integer lessonId = super.create(lesson, creator);

        RemoteLogHelper.log(LogModule.CHAPTER_LESSON.getCode(), creator
                , "[完成]创建" + moduleCN + "[节ID：{}/序号：{}]({})"
                , lesson.getChapterId(), lesson.getRank(), lessonId);
        return lessonId;
    }

}
